﻿#ERROR LOG WRITER
function WriteLog($message,$append) 
{
	$logname = "Your Log Name Here"; 
	$logsource = "Your Log Source Here"; 
	$networklog = "\\logpath\$env:USERNAME-$env:COMPUTERNAME.log";
	$machinelog = "$env:windir\temp\machine_log_name_here.log";

    #create a new event log entry in the Windows Event Log if needed
    $logFileExists = [System.Diagnostics.EventLog]::Exists($logname);
    if (! $logFileExists) 
    {
        New-EventLog -LogName $logname -Source $logsource;
    }
    else
    {
        $logSourceExists = [System.Diagnostics.EventLog]::SourceExists($logsource);
        if(! $logSourceExists)
        {
            New-EventLog -LogName $logname -Source $logsource;
        }
    }

    $date = Get-Date;
    if($append -ne 1) 
    {
        #overwrite existing contents
        "$date - $message" | Out-File $networklog;
        "$date - $message" | Out-File $machinelog; 
    }
    else
    {
        #append to existing contents
        "$date - $message" | Out-File $networklog -Append;
        "$date - $message" | Out-File $machinelog -Append;
    }
    #write to windows event log
    Write-EventLog –LogName $logname –Source $logsource –EntryType Information –EventID 1  –Message “$message”;
}

#
#	EXAMPLES
#
#	Append to log: 
#	WriteLog("Your Message Here",1);
#
#	Do not append to log:
#	WriteLog("Your Message Here");
#
